<template>
  <div>
    <div class="swiper">
      <!-- 轮播图 -->
      <el-carousel :interval="4000"
                   type="card"
                   height="200px">
        <el-carousel-item v-for="(item,index) in banners "
                          :key="index">
          <img :src="item.imageUrl"
               alt=""
               class="swiper-img"
               @click="open(item.url)" />
        </el-carousel-item>
      </el-carousel>
    </div>
    <!-- 推荐歌单 -->
    <div class="recommend-list"
         v-if="recommends!==''">
      <h3><router-link :to="{path:'/songlist'}">推荐歌单<i class="el-icon-arrow-right"></i></router-link></h3>
      <div class="img-list">
        <ul>
          <li v-for="(item) in recommends"
              :key="item.id">
            <router-link :to="{path:'/homeitem/:id',query:{id:item.id}}">
              <div class="imgs">
                <img v-lazy="item.picUrl"
                     :key="item.picUrl">
              </div>
              <div class="num">
                <span class="play">{{(subplaycount(item))|  changeNum }}</span>
                <svg class="icon cc"
                     aria-hidden="true">
                  <use xlink:href="#icon-bofang"></use>
                </svg>
              </div>
              <div class="icon-size">
                <svg class="icon"
                     aria-hidden="true">
                  <use xlink:href="#icon-gf-play"></use>
                </svg>
              </div>
              <div class="pingyu">
                <span>{{ item.name }}</span>
              </div>
            </router-link>
          </li>
        </ul>
      </div>
    </div>
    <!--最新音乐-->
    <div class="newmusic-list mtop-10"
         v-if="newsongs!==''">
      <h3><router-link :to="{path:'/newmusic'}">最新音乐<i class="el-icon-arrow-right"></i></router-link></h3>
      <div class="newmusic">
        <ul>
          <li v-for="(item,index) in newsongs"
              :key="index"
              @click="playlist(index,item.id)">
            <div>
              <img v-lazy="item.picUrl"
                   :key="item.picUrl">
              <div class="icon-size">
                <svg class="icon"
                     aria-hidden="true">
                  <use xlink:href="#icon-gf-play"></use>
                </svg>
              </div>
              <span>{{ item.name }}</span>
              <span style="font-size:12px;color:grey">{{ item.song.artists[0].name }}</span>
            </div>
          </li>
        </ul>
      </div>
    </div>
    <!-- 推荐mv -->
    <div class="remv-list mtop-20"
         v-if=" recommendmvs!==''">
      <h3>推荐Mv<i class="el-icon-arrow-right"></i></h3>
      <mvList :list="recommendmvs"
              type="picUrl"></mvList>
    </div>
  </div>
</template>
<script>
import mvList from '@/components/mvList/mvList'
import { getRecommendMv, getBanner, getPersonalized, getNewsong } from "@/api/discovery"
import { getRePlaylist } from '@/api/RankList'
import { mapMutations, mapState } from "vuex"
export default {
  name: 'Discovery',
  // 组件挂载完毕，可以向服务器发请求
  created () {
    this.getNewsong()
    this.getRecommendMv()
    this.getBanner()
  },
  components: { mvList },
  data () {
    return {
      recommendmvs: [],//推荐mv
      banners: [],//轮播图
      recommends: [],//推荐歌单
      newsongs: {},//最新音乐
    }
  },
  computed: {
    ...mapState({
      isLogin: (state) => {
        return state.user.isLogin
      },
    }),
  },
  methods: {
    ...mapMutations(['updatedPlayList', 'updatedPlaylistIndex']),
    open (url) {
      if (url !== null) window.open(url)
    },
    // 最新音乐
    async getNewsong () {
      let res = await getNewsong()
      if (res.code !== 200) return
      this.newsongs = res.result
    },
    // 通知底部组件播放
    playlist (index) {
      // 封装传回去的playlist
      let al = this.newsongs[index]
      this.newsongs[index]["al"] = al
      let ar = [{ name: this.newsongs[index].song.artists[0].name }]
      this.newsongs[index]["ar"] = ar
      this.updatedPlayList(this.newsongs)
      this.updatedPlaylistIndex(index)
      //绑定全局事件总线‘currentId’
      this.$bus.$emit('currentId', this.newsongs[index].id)
    },
    // 推荐歌单
    async getPersonalized () {
      let res = await getPersonalized()
      if (res.code !== 200) return
      this.recommends = res.result
    },
    // 登录后的推荐歌单
    async getRePlaylist () {
      let res = await getRePlaylist()
      if (res.code !== 200) return
      this.recommends = res.recommend
    },
    // 获取轮播图
    async getBanner () {
      let res = await getBanner()
      if (res.code !== 200) return
      this.banners = res.banners
    },
    // 获取推荐mv
    async getRecommendMv () {
      let res = await getRecommendMv()
      if (res.code !== 200) return
      this.recommendmvs = res.result
    },
    // 处理playcount
    subplaycount (item) {
      if (item.type == 1) {
        return item.playcount
      } else {
        return item.playCount
      }
    }
  },
  watch: {
    // 如果isLogin发生变化则替换recommends
    isLogin: {
      immediate: true,
      handler (val) {
        if (val !== true) {
          this.getPersonalized()
        } else {
          this.getRePlaylist()
        }
      }
    }
  }
}
</script>

<style scoped lang="less">
div {
  li:hover {
    cursor: pointer;
  }
  .swiper {
    margin-left: 70px;
    width: 1200px;
    .swiper-img {
      height: 210px;
      border-radius: 8px;
    }
  }
  .recommend-list {
    overflow: hidden;
    h3 {
      margin-left: 80px;
    }
    i {
      margin-left: 5px;
    }
    .img-list {
      width: 1200px;
      margin-left: 50px;
      ul {
        display: flex;
        flex-wrap: wrap;
        margin-top: 10px;
        li {
          width: 200px;
          height: 253px;
          margin-left: 30px;
          position: relative;
          .imgs {
            width: 200px;
            height: 210px;
            img {
              width: 200px;
              height: 210px;
              border-radius: 8px;
            }
          }
          .num {
            position: absolute;
            top: 8px;
            width: 192px;
            height: 30px;
            left: 0px;
            .play {
              text-align: right;
              float: right;
              width: auto;
              height: 20px;
              font-size: 12px;
              color: #fff;
            }
            .cc {
              margin-top: -2px;
              float: right;
              width: 20px;
              height: 20px;
            }
          }
          .icon-size {
            opacity: 0;
            position: absolute;
            top: 165px;
            left: 150px;
            width: 36px;
            height: 36px;
            border-radius: 18px;
            background-color: #fff;
            .icon {
              width: 20px;
              height: 20px;
              margin-left: 8px;
              margin-top: 8px;
            }
          }
          .pingyu {
            span {
              display: block;
              float: left;
              width: 180px;
              height: 20px;
              font-size: 14px;
              overflow: hidden;
              margin-top: 5px;
            }
          }
        }
        li:hover .icon-size {
          transition: all 1s;
          opacity: 1;
        }
      }
    }
  }
  .newmusic-list {
    overflow: hidden;
    h3 {
      margin-left: 80px;
    }
    i {
      margin-left: 5px;
    }
    .newmusic {
      width: 1200px;
      margin-left: 60px;
      ul {
        display: flex;
        flex-wrap: wrap;
        margin-top: 10px;
        li {
          display: block;
          float: left;
          width: 360px;
          height: 60px;
          margin-top: 10px;
          margin-left: 20px;
          border-radius: 4px;
          div {
            position: relative;
            img {
              float: left;
              width: 60px;
              height: 60px;
              border-radius: 4px;
            }
            .icon-size {
              opacity: 1;
              position: absolute;
              top: 17px;
              left: 17px;
              width: 26px;
              height: 26px;
              border-radius: 13px;
              background-color: #fff;
              .icon {
                width: 12px;
                height: 12px;
                margin-left: 8px;
                margin-top: 7px;
              }
            }
            span {
              display: block;
              height: 30px;
              line-height: 30px;
              overflow: hidden;
              padding-left: 15px;
              font-size: 13px;
            }
          }
        }

        li:hover {
          background-color: #ebebeb;
        }
      }
    }
  }
  .remv-list {
    margin-left: 60px;
    h3 {
      margin-left: 20px;
    }
    i {
      margin-left: 5px;
    }
  }
}
</style>